

***********************************************************************************
/**** This do-file runs regressions on RR policy on the comprehensive sample and builds the multiple bank relationship sample at the end *****/
**********************************************************************************

/* In details, this do-file builds Table 3. In this table, we relax credit demand controls - with firm fixed effects - and run regressions on single bank relationship firms, expanding the sample used in Table 2 (restricted to multiple bank relationship fimrs). This exercise alleviates concerns about sample bias.     */
 

*********************************
* Loading ADOs and Pseudo DTA
********************************
clear all
clear matrix
clear mata
clear results
set more off, permanently

* There is nothing special in my ado folder. We consider reghdfe, outreg2, center and unique (as well as its dependencies) enough to run this code

adopath + "//sbcdf060/Depep01$/ado"
adopath ++ "//sbcdf060/Depep01$/ado"
global dta_file = "D:\Countercyclical\Dta_files"
global output_file = "D:\Countercyclical\Output_files"


* This file contains Pseudo Data for the two RR episodes (easing and tightening)
use "$dta_file/PseudoData_RR.dta", clear



/***********************************
**** Prepare the working sample ****
***********************************/

** Eliminate defaulted loans
drop if L3_firm_default==1

* Defining loan-level dependent
local dep_var wd3ln_amount_bank


* Building max_bank
gen abank= exp(ln_L3_amount_bank) 
bysort firm_id time: egen afirm2 = sum(abank)
gen afirm= exp(ln_L3_amount_firm) 
gen mshare2= abank/afirm
bysort firm_id time: egen mx_bank=max(mshare2)
gen mmx_bank_id=bank_id if mx_bank == mshare2
bysort firm_id time: egen max_bank=max(mmx_bank)
drop mmx_bank mx_bank abank afirm*
drop mshare2 

* Winsorizing
winsor2 dln3_amount_bank dln3_amount_firm L3_capital L3_liquidity L3_ln_size L3_npl L3_scline L3_ln_r_maturity L3_total_interest ln_L3_amount_firm L3_ln_employees RReasing RRtightening, c(1 99)

ren (dln3_amount_bank_w dln3_amount_firm_w L3_capital_w L3_liquidity_w L3_ln_size_w L3_npl_w L3_scline_w L3_ln_r_maturity_w L3_total_interest_w ln_L3_amount_firm_w L3_ln_employees_w RReasing_w RRtightening_w) (wd3ln_amount_bank wd3ln_amount_firm wL3_capital wL3_liquidity wL3_ln_size wL3_npl wL3_scline wL3_ln_r_maturity wL3_total_interest wln_L3_amount_firm wL3_ln_employees wRReasing wRRtightening)

* Defining controls (the same from Table 2)
global dep_var		    wd3ln_amount_bank 

global bank_controls   wL3_capital wL3_liquidity wL3_ln_size  gov foreign small commercial wL3_npl  

global risk_controls         L3_collateral wL3_scline L3_foreign_currency wL3_ln_r_maturity wL3_total_interest

global firm_controls    wln_L3_amount_firm wL3_ln_employees   

global clusters         bank firm_industry2

*** Creating firms above and below the median of risk and size to split samples
bysort firm_id time: egen L3_firm_interest = pctile(L3_total_interest), p(50)
	

* Set-up sample so that the counting of cases is constant at firm and loan-level 
foreach d of local dep_var {


reghdfe `d'   $risk_controls  c.wRReasing#c.wL3_total_interest if time=="easing", absorb(max_bank#bank_id max_bank#sec2mun) res(easing)

reghdfe `d'   $risk_controls  c.wRRtightening#c.wL3_total_interest  if time=="tightening", absorb(max_bank#bank_id max_bank#sec2mun) res(tightening)


keep if !mi(easing)|!mi(tightening)
drop easing tightening

}

preserve

      tempfile g

		bysort firm_id time: gen first = _n==1
		keep if first==1
		drop first
		
local dep_var $dep_var

foreach d of local dep_var {

reghdfe `d'   $risk_controls  c.wRReasing  if time=="easing", absorb(max_bank sec2mun) res(easing)
reghdfe `d'   $risk_controls  c.wRRtightening  if time=="tightening", absorb(max_bank sec2mun) res(tightening)

keep if !mi(easing)|!mi(tightening)


	}
     save `g', replace


restore 
merge m:1 firm_id time using `g', keepusing(easing tightening) keep(match) nogen
* Sample Ready

                

            ******************************************************************************
**          Building Table 3 : loan level regressions with RRs on the comprehensive sample
            ******************************************************************************
gen cons = 10			
gen n_firms=0
gen n_sectors=0

cd $output_file
local dep_var $dep_var	 
local time_var easing tightening



foreach d of local dep_var {
foreach t of local time_var {
	


unique firm_id if !mi(`t')
replace n_firms = r(sum)

unique firm_industry2 if !mi(`t')
replace n_sectors = r(sum)



sum n_firms if !mi(`t')
local n_firms = r(mean)

sum n_sectors if !mi(`t')
local n_sectors = r(mean)


gen RR = cond("`t'" == "easing", wRReasing, wRRtightening) 


* Columns 1 and 6
reghdfe `d' RR  if !mi(`t') , absorb(cons) vce(cluster $clusters)
outreg2 using Table3, excel append label ctitle(`t')  dec(3) ///
 addtext(Loan and Firm Controls, No, Sector*County FE, No, ///
N firms, `n_firms',  N sectors, `n_sectors') nocons

* Columns 2 and 7
reghdfe `d' RR $firm_controls $risk_controls    if !mi(`t'), absorb(firm_industry2) vce(cluster $clusters)
outreg2 using Table3, excel append label ctitle(`t')  dec(3) ///
 addtext(Loan and Firm Controls, Yes, Sector*County FE, No, ///
N firms, `n_firms',  N sectors, `n_sectors') drop( $firm_controls $risk_controls) nocons
 
 * Columns 3 and 8
reghdfe `d' RR $firm_controls $risk_controls   if !mi(`t'), absorb(sec2mun) vce(cluster $clusters)
outreg2 using Table3, excel append label ctitle(`t')  dec(3) ///
 addtext(Loan and Firm Controls, Yes, Sector*County FE, Yes, ///
N firms, `n_firms',  N sectors, `n_sectors') drop( $firm_controls $risk_controls) nocons

 * Columns 4 and 9
 reghdfe `d' RR $risk_controls $firm_controls $bank_controls    if !mi(`t') , absorb(firm_industry2) vce(cluster $clusters)
outreg2 using Table3, excel append label ctitle(`t')  dec(3) ///
 addtext(Loan and Firm Controls, Yes, Sector*County FE, No, ///
N firms, `n_firms',  N sectors, `n_sectors') drop( $firm_controls $risk_controls) nocons

  * Columns 5 and 10
 reghdfe `d' RR $risk_controls $firm_controls $bank_controls    if !mi(`t') , absorb(sec2mun) vce(cluster $clusters)
outreg2 using Table3, excel append label ctitle(`t')  dec(3) ///
 addtext(Loan and Firm Controls, Yes, Sector*County FE, Yes, ///
N firms, `n_firms',  N sectors, `n_sectors') drop( $firm_controls $risk_controls) nocons

drop RR

}
}


******************************************************************************
**          Building Multiple Bank Relationship Data ******************************************************************************

drop easing tightening

*** Adjusting the sample for fixed effects. Only multiple bank relationship firms will remain in the new DTA. 

/* Because of the random nature of the Pseudo Data, this sample will be too small after the introduction of Firm Fixed effects. Thus, we make changes to introdue more pseudo bank relationships (decreasing the number of firms) before restricting the sample to multiple bank relationship firms below. 
*/

replace firm_id = runiformint(1,350)
*replace municipality = runiformint(1,200)
*egen sec_mun = group(firm_industry2 municipality)
*replace sec_mun = runiformint(1,160)
bysort firm_id: egen sec2_mun = max(sec2mun)
drop sec2mun


preserve 	

	tempfile z
	
	bysort firm_id time: gen first=_n==1
	keep if first==1
		
	
	bysort time : egen med_interest = pctile(L3_firm_interest), p(50)
	
	* Defining risky firms as above the median of paid interest rates before the policy
	gen  riskP50= ((L3_firm_interest - med_interest)>0)

	* Defining small firms as below the median of number of employees before the policy
	bysort time : egen med_emp = pctile(n_employees), p(50)

	gen  empP50= ((n_employees - med_emp)<0)
    drop first
	save `z', replace

restore 

merge m:1 firm_id time using `z', nogen keep(match)


*Restricting the sample at the loan-level 
local dep_var wd3ln_amount_bank
foreach d of local dep_var {

reghdfe `d'   $risk_controls    if time=="easing", absorb(firm_id sec2_mun bank_id) res(easing)
reghdfe `d'   $risk_controls   if time=="tightening", absorb(firm_id sec2_mun bank_id ) res(tightening)



keep if !mi(easing)|!mi(tightening)
drop easing tightening

}

*Restricting the sample at the firm-level to keep the same firms at the loan and firm-level regressions
drop max_bank

preserve

      tempfile g
	  gen abank= exp(ln_L3_amount_bank) 
				bysort firm_id time: egen afirm2 = sum(abank)
				gen afirm= exp(ln_L3_amount_firm) 
				gen mshare2= abank/afirm
				bysort firm_id time: egen mx_bank=max(mshare2)
				gen mmx_bank_id=bank if mx_bank == mshare2
				bysort firm_id time: egen max_bank=max(mmx_bank)
				drop mmx_bank mx_bank
				drop mshare2
		bysort firm_id time: gen first = _n==1
		keep if first==1
		drop first
		
local dep_var wd3ln_amount_bank

foreach d of local dep_var {

	reghdfe `d'   $risk_controls $firm_controls $bank_controls   if time=="easing", absorb(sec2_mun#riskP50#empP50 ) res(easing)
	reghdfe `d'   $risk_controls  $firm_controls $bank_controls   if time=="tightening", absorb(sec2_mun#riskP50#empP50) res(tightening)



	keep if !mi(easing)|!mi(tightening)
	
	}
     save `g', replace


restore 
merge m:1 firm_id time using `g', keepusing(easing tightening) keep(match) nogen



save "$dta_file/PseudoData_RRmbr.dta", replace
